//
//  CaseIntroduceViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/18.
//  Copyright © 2017年 test. All rights reserved.
//

#import "CaseIntroduceViewController.h"
#import "PublicClass.h"
#import "CaseIntroduceCell.h"
#import "ValuePickerView.h"

@interface CaseIntroduceViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    UIButton *SureButton;
    UIView *headView;
    UIView *footerView;
    
    NSArray *rightButtonArray;
    NSArray *phaseArray;//阶段
    NSArray *statusArray;//地位
    
    ValuePickerView *pickerV;
}

@property (nonatomic, strong)UILabel *accoutNumber;
@property (nonatomic, strong)UIView *NavView;
@property (nonatomic, strong) UITextField *noteField;//案件备注
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)UIButton *phaseButton;
@property (nonatomic, strong)UIButton *statusButton;
@property (nonatomic,strong)NSMutableArray *dataSource1;
@property (nonatomic,strong)NSMutableArray *dataSource2;

@end

@implementation CaseIntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = GetColor(255,210,58,1);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataSource1 = [NSMutableArray arrayWithObjects:@"收案日期：",@"案由：",@"委托人：",@"对方当事人：",@"受理部门：",@"诉讼标的：",@"代理费：",@"诉讼阶段：",@"诉讼地位：",@"办案地区：",@"案件备注：", nil];
    

    [self initData];
    [self.view addSubview:self.tableView];
    //键盘通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(up:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(down:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)initData{
    
    pickerV = [ValuePickerView new];
    phaseArray = @[@"调查取证",@"一审",@"二审",@"执行"];
    statusArray = @[@"1",@"2",@"3",@"4",@"5"];
}

- (void)AboutPhaseButton:(UIButton *)button
{
    pickerV.dataSource = phaseArray;
    pickerV.pickerTitle = @"请选择";
    pickerV.valueDidSelect = ^(NSString *value) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        NSArray * stateArr = [value componentsSeparatedByString:@"/"];
        [button setTitle:stateArr[0] forState:UIControlStateNormal];
    };
    [pickerV show];
}
- (void)AboutstatusButton:(UIButton *)button
{
    pickerV.dataSource = statusArray;
    pickerV.pickerTitle = @"q请选择";
    pickerV.valueDidSelect = ^(NSString *value) {
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        NSArray * stateArr = [value componentsSeparatedByString:@"/"];
        [button setTitle:stateArr[0] forState:UIControlStateNormal];
    };
    [pickerV show];
}

- (void)AboutSureButton:(UIButton *)button{
    if ([self judgeSubmitInfo]) {
        
    }
    
}
#pragma mark ———— 判断提交信息
- (BOOL)judgeSubmitInfo
{
    for (UIButton *button in rightButtonArray) {
        
        if ([button.titleLabel.text rangeOfString:@"请"].location != NSNotFound) {
            [self warnWitherrorInfo:@"提交信息不完善"];
            return NO;
        }
    }
    return YES;
}
#pragma mark - 创建tableFooterView
- (UIView *)createFooterView{
    
    
    footerView = [[UIView alloc] init];
    footerView.frame = CGRectMake(0,0, MAINSCREEN_WIDTH, ADAPTATION_HEIGHT(40));
    footerView.backgroundColor = GetColor(241, 241, 241, 1);
    //提交按钮
    SureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    SureButton.frame = CGRectMake(0, 0, ADAPTATION_WIDTH(355), ADAPTATION_HEIGHT(26));
    SureButton.center = footerView.center;
    SureButton.backgroundColor = GetColor(115, 187, 238, 1);
    [SureButton setTitle:@"确认提交" forState:UIControlStateNormal];
    SureButton.titleLabel.font = [UIFont systemFontOfSize:14/W];
    SureButton.layer.cornerRadius = 5;
    SureButton.layer.masksToBounds = YES;
    [SureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [SureButton addTarget:self action:@selector(AboutSureButton:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:SureButton];
    return footerView;
}

#pragma mark - tableViewDelegate
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MAINSCREEN_WIDTH, MAINSCREEN_HEIGHT-64-49) style:UITableViewStylePlain];
        _tableView.separatorInset = UIEdgeInsetsZero;
        _tableView.backgroundColor = GetColor(241, 241, 241, 1);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = YES;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = headView;
        _tableView.tableFooterView = [self createFooterView];
        //        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView registerClass:[CaseIntroduceCell class] forCellReuseIdentifier:@"AboutMineCell"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource1.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ADAPTATION_HEIGHT(55);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseIntroduceCell *personalCell = [tableView dequeueReusableCellWithIdentifier:@"AboutMineCell" forIndexPath:indexPath];
    if (indexPath.row == 7 || indexPath.row == 8) {
        personalCell.bgView.backgroundColor = [UIColor clearColor];
    }
    personalCell.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
    personalCell.PersonalListLabel.text = self.dataSource1[indexPath.row];
    personalCell.PerArticleLabel.text = @"5656565656";
    personalCell.selectionStyle = UITableViewCellSeparatorStyleNone;
    personalCell.accessoryType = UITableViewCellAccessoryNone;
    
    if (indexPath.row == 7) {
        personalCell.PerArticleLabel.hidden = YES;
        _phaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _phaseButton.frame = CGRectMake(ADAPTATION_WIDTH(90), ADAPTATION_HEIGHT(15), ADAPTATION_WIDTH(120), ADAPTATION_HEIGHT(26));
        _phaseButton.backgroundColor = [UIColor lightGrayColor];
        [_phaseButton setTitle:@"请选择" forState:UIControlStateNormal];
        _phaseButton.titleLabel.font = [UIFont systemFontOfSize:14/W];
        _phaseButton.layer.cornerRadius = 5;
        _phaseButton.layer.masksToBounds = YES;
        [_phaseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_phaseButton addTarget:self action:@selector(AboutPhaseButton:) forControlEvents:UIControlEventTouchUpInside];
        [personalCell addSubview:_phaseButton];
        
    }
    if (indexPath.row == 8) {
        personalCell.PerArticleLabel.hidden = YES;
        _statusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _statusButton.frame = CGRectMake(ADAPTATION_WIDTH(90), ADAPTATION_HEIGHT(15), ADAPTATION_WIDTH(120), ADAPTATION_HEIGHT(26));
        _statusButton.backgroundColor = [UIColor lightGrayColor];
        [_statusButton setTitle:@"请选择" forState:UIControlStateNormal];
        _statusButton.titleLabel.font = [UIFont systemFontOfSize:14/W];
        _statusButton.layer.cornerRadius = 5;
        _statusButton.layer.masksToBounds = YES;
        [_statusButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_statusButton addTarget:self action:@selector(AboutstatusButton:) forControlEvents:UIControlEventTouchUpInside];
        [personalCell addSubview:_statusButton];
        rightButtonArray = @[_phaseButton,_statusButton];
        
    }
    if (indexPath.row == 10) {
        personalCell.PerArticleLabel.hidden = YES;
        _noteField = [[UITextField alloc] initWithFrame:CGRectMake(ADAPTATION_WIDTH(90), ADAPTATION_HEIGHT(18), ADAPTATION_WIDTH(240), ADAPTATION_HEIGHT(20))];
        _noteField.backgroundColor = [UIColor clearColor];
        _noteField.textAlignment = NSTextAlignmentLeft;
        _noteField.layer.cornerRadius = 7;
        _noteField.clipsToBounds = YES;
        _noteField.delegate = self;
        _noteField.placeholder = @"请填写案件备注";
        _noteField.font = [UIFont systemFontOfSize:14/W];
        _noteField.keyboardType = UIKeyboardTypeDefault;
        _noteField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _noteField.leftViewMode = UITextFieldViewModeAlways;
        [personalCell addSubview:_noteField];
        
    }
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(ADAPTATION_WIDTH(10), ADAPTATION_HEIGHT(54), MAINSCREEN_WIDTH, ADAPTATION_HEIGHT(1));
    label.backgroundColor = GetColor(215, 215, 215, 1);
    [personalCell addSubview:label];
    
    return personalCell;
}

//警告框
- (void)warnWitherrorInfo:(NSString *)error{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/2/W, ADAPTATION_HEIGHT(60))];
    label.backgroundColor = [UIColor lightGrayColor];
    label.center = self.view.center;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:18/W];
    label.layer.cornerRadius = 15;
    label.layer.masksToBounds = YES;
    label.numberOfLines = 0;
    label.text = error;
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [UIView animateWithDuration:3.0 animations:^{
        label.alpha = 0.0;
    } completion:^(BOOL finished) {
        
        [label removeFromSuperview];
    }];
    
}
#pragma mark - 键盘通知
- (void)up:(NSNotification *)info{
    [UIView animateWithDuration:0.8 animations:^{
        self.view.center = CGPointMake(self.view.center.x, MAINSCREEN_HEIGHT/2 - ADAPTATION_HEIGHT(254));
    }];
}
- (void)down:(NSNotification *)info{
    [UIView animateWithDuration:0.8 animations:^{
        self.view.center = CGPointMake(self.view.center.x, MAINSCREEN_HEIGHT/2);
    }];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [_noteField resignFirstResponder];
}

@end
